/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.institution.approval;

import com.alibaba.fastjson.JSONObject;
import com.koron.audit.approval.AfterApprovalService;
import com.koron.audit.domain.Audit;
import com.koron.institution.bean.InstitutionConformanceAssessment;
import com.koron.institution.bean.InstitutionConformanceAssessmentItem;
import com.koron.institution.mapper.*;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ConformanceAssessmentAuditService implements AfterApprovalService {

    private final static  Logger logger = LoggerFactory.getLogger(InstitutionAddService.class);

    @Override
    public void editApproval(Audit audit) {
        SessionFactory factory = new SessionFactory();
        try {
            List<String> idList = JSONObject.parseArray(audit.getBusinessIds(), String.class);
            if (idList != null && !idList.isEmpty()) {
                String planId = idList.get(0);
                if (StringUtils.isNotEmpty(planId)) {
                    InstitutionConformanceAssessmentMapper mapper = factory.getMapper(InstitutionConformanceAssessmentMapper.class);
                    InstitutionConformanceAssessment assessment = mapper.selectByPrimaryKey(planId);
                    InstitutionConformanceAssessmentItemMapper itemMapper = factory.getMapper(InstitutionConformanceAssessmentItemMapper.class);
                    InstitutionFileMapper fileMapper = factory.getMapper(InstitutionFileMapper.class);
                    if (assessment != null) {
                        List<InstitutionConformanceAssessmentItem> itemList = itemMapper.selectByConformanceAssessmentId(assessment.getId());
//                        if (itemList != null && !itemList.isEmpty()) {
//                            List<String> institutionIdList = new ArrayList<>();
//                            for (InstitutionConformanceAssessmentItem item : itemList) {
//                                institutionIdList.add(item.getInstitutionId());
//                            }
//                            //更新为已评估;
//                            if (institutionIdList != null && !institutionIdList.isEmpty()) {
//                                fileMapper.updateReviseState(institutionIdList);
//                            }
//                        }
                        InstitutionConformanceAssessment assessmentNew = new InstitutionConformanceAssessment();
                        assessmentNew.setState(2);
                        assessmentNew.setId(assessment.getId());
                        mapper.updateByPrimaryKeySelective(assessmentNew);
                    }
                }
            }
            factory.close();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            if (null != factory)
                factory.close(false);
        }
    }

    @Override
    public void discardApproval(Audit audit) {

    }

    @Override
    public void processDiscard(Audit audit) {

    }

    @Override
    public void reject(Audit audit, List<String> userIdList) {
        // TODO 流程驳回操作
    }

    @Override
    public void passApproval(Integer operation, Audit audit) {
        // TODO 流程提交成功操作
    }
}
